home *** CD-ROM | disk | FTP | other *** search
/ Super PC 34 / Super PC 34 (Shareware).iso / spc / UTIL / DJGPP2 / V2 / DJLSR200.ZIP / src / libc / posix / fcntl / open.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-27  |  1.5 KB  |  62 lines

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. #include <libc/stubs.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <errno.h>
  7. #include <fcntl.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10. #include <io.h>
  11.  
  12. #include <libc/dosio.h>
  13.  
  14. int
  15. open(const char* filename, int oflag, ...)
  16. {
  17.   int fd, dmode, bintext;
  18.  
  19.   /* Check this up front, to reduce cost and minimize effect */
  20.   if ((oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  21.     if (__file_exists(filename))
  22.     {
  23.       /* file exists and we didn't want it to */
  24.       errno = EEXIST;
  25.       return -1;
  26.     }
  27.  
  28.   /* figure out what mode we're opening the file in */
  29.   bintext = oflag & (O_TEXT | O_BINARY);
  30.   if (!bintext)
  31.     bintext = _fmode & (O_TEXT | O_BINARY);
  32.   if (!bintext)
  33.     bintext = O_BINARY;
  34.  
  35.   /* DOS doesn't want to see these bits */
  36.   oflag &= ~(O_TEXT | O_BINARY);
  37.  
  38.   dmode = (*((&oflag)+1) & S_IWUSR) ? 0 : 1;
  39.  
  40.   fd = _open(filename, oflag & 0xff);    /* only low byte used */
  41.   if (fd == -1 && oflag & O_CREAT)
  42.     fd = _creat(filename, dmode);
  43.  
  44.   if (fd == -1)
  45.     return fd;    /* errno already set by _open or _creat */
  46.  
  47.   if (oflag & O_TRUNC)
  48.     if (_write(fd, 0, 0) < 0)
  49.       return -1;
  50.  
  51.   /* we do this last because _open and _create set it also. */
  52.   /* force setmode() to do ioctl() for cooked/raw */
  53.   __file_handle_set(fd, bintext ^ (O_BINARY|O_TEXT));
  54.   /* this will do cooked/raw ioctl() on character devices */
  55.   setmode(fd, bintext);
  56.  
  57.   if(oflag & O_APPEND)
  58.     lseek(fd, 0, SEEK_END);
  59.  
  60.   return fd;
  61. }
  62.